#include "myhead.h"

#define SERVER_IP "192.168.13.25"
#define SERVER_PORT 50000


int speed = 0;//远程倍速切换=1表示2倍，=0表示正常

//连接服务器
int connectToServer() 
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) 
	{
        perror("socket");
        return -1;
    }

    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SERVER_PORT);
    if (inet_pton(AF_INET, SERVER_IP, &(server_addr.sin_addr)) <= 0) 
	{
        perror("inet_pton");
        close(sockfd);
        return -1;
    }

    if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) 
	{
        perror("connect");
        close(sockfd);
        return -1;
    }
    
    return sockfd;
}

int main(int argc, char const *argv[])
{
    //初始化
    Init();
    //链表初始化
    video_head = List_Init(argv[1]);//视频链表
    count = 0;
    pD_music music_head = List_Init(argv[2]);//音乐链表
    music = music_head;
    // video = video_head;
    while(1)
    {
        //显示主界面
        ShowJpg(map, "pic/UI/main.jpg" , 0 , 0 , 1 , 1 );
        //获取坐标  点击进入功能界面
        Get_side();
		//游戏
		if(x_s > 125 && x_s < 273 && y_s > 120 && y_e < 350)
		{
			//用户点击了游戏功能
			printf("欢迎你进入游戏\n");
			 while (1)
			{
				ShowJpg(map , "Game_Pic/Game_UI.jpg" , 0 , 0 , 1 , 1 );

				//获取坐标
				Get_side();

				//点击了 “2048”
				if(x_s > 60 && x_s < 210 && y_s > 130 && y_e < 280)
				{
					printf("欢迎进入2048\n");
					Into2048(map);
				}
				else if(x_s > 0 && x_s < 100 && y_s > 0 && y_e < 60)//点击了 “返回”
				{
					printf("返回成功");
					break;
				}
			}
		}
		//视频
        else if(x_s > 80 && x_s < 800 && y_s > 80 && y_e < 480)
        {
            while (1)
            {
                //显示功能界面
                ShowJpg(map, "pic/UI/menu.jpg" , 0 , 0 , 1 , 1 );
                Get_side();								
							
                //手机客户端操作视频
                if(x_s > 335 && x_s < 438 && y_s > 120 && y_e < 350)
                {
                    if(music_init == 1)
                    {
                        video_num = 1;
                    }
                    music_init = 0;
                    printf("欢迎你进入视频\n");
                    //播放界面初始化
                    Music_InterFace_Init();
                    //常量初始化
                    Data_Init();
                    printf("初始化成功\n");
                    //遍历视频链表
                    List_DisPlay(video_head);
					
					//连接服务器
					int sockfd = connectToServer();
					if (sockfd == -1) 
					{
						perror("Connect To Server error");
						break;
					}

					while (1) 
					{
						// 接收服务器信息	
						memset(recvbuffer, 0, sizeof(recvbuffer));
						ssize_t bytesRead = recv(sockfd, recvbuffer, sizeof(recvbuffer), 0);
						
						if (bytesRead <= 0) 
						{
							// 处理连接断开等情况
							printf("断开连接\n");
							break;
						}
						//检测是否有换行符
						char *p = strchr(recvbuffer, '\n');
						if(p != NULL)
						{
							p[0] = '\0'; 
						}
						printf("%s---%ld\n", recvbuffer, strlen(recvbuffer));
						usleep(50000);
						if(strcmp(recvbuffer,"1") == 0)
							{
								usleep(50000);
								printf("执行播放/暂停功能\n");
								Music_Play(video_head);				
							}
							//上一首
							else if((strcmp(recvbuffer,"2") == 0) && mplayer == 1)
							{								
								usleep(50000);
								printf("播放上一首\n");
								Music_prev(video_head);
							}
							//下一首
							else if((strcmp(recvbuffer,"3") == 0) && mplayer == 1)
							{
								usleep(50000);
								printf("播放下一首\n");
								Music_Next(video_head);
							}
							//后退
							else if((strcmp(recvbuffer,"4") == 0) && mplayer == 1)
							{
								usleep(50000);
								printf("后退5s\n");
								Music_Seek(-5);
							}
							//快进
							else if((strcmp(recvbuffer,"5") == 0) && mplayer == 1)
							{
								usleep(50000);
								printf("快进5s\n");
								Music_Seek(5);
							}
							//上一页
							else if((strcmp(recvbuffer,"6") == 0) && mplayer == 1)
							{
								usleep(50000);
								Prev_List(&musiclist , video_head);
							}
							//下一页
							else if((strcmp(recvbuffer,"7") == 0) && mplayer == 1)
							{
								usleep(50000);
								Next_List(&musiclist , video_head);
							}
							//倍速
							else if((strcmp(recvbuffer,"8") == 0) && mplayer == 1)
							{
								if (speed == 0) 
								{
								printf("2倍速\n");
								Send_Command("speed_set 2\n");
								speed = 1; 
								} 
								else 
								{
								printf("1倍速\n");
								Send_Command("speed_set 1\n");
								speed = 0; 
								}
							}
							//静音
							else if((strcmp(recvbuffer,"9") == 0) && mplayer == 1)
							{
								printf("静音/恢复\n");
								Mute_Volume();
							}
						
							//返回
							else if(strcmp(recvbuffer,"10") == 0)
							{
								//用户点击了返回
								printf("点击了返回\n");
								music_init = 0;
								//退出
								ExitTid();
								break;
							}       		
					}	
					// 关闭socket
					close(sockfd);               
				}
                //触摸屏操作音乐
                else if(x_s > 532 && x_s < 665 && y_s > 120 && y_e < 350)
                {
                    if(music_init == 0)
                    {
                        video_num = 1;
                    }
                    music_init = 1;
                    printf("欢迎你进入音乐\n");
                    //播放界面初始化
                    Music_InterFace_Init();
					//常量初始化
                    Data_Init();
                    //遍历音乐链表
                    List_DisPlay(music_head);	
					
					//创建线程5：歌词显示				
					if(pthread_create(&tid5,NULL,pthread_show_lrc,NULL) == -1)
					{
						perror("pthread_create fail");
						return -1;
					}
                    				
                    while (1)
                    {												
                       //获取坐标，判断动作
                        Get_side();						
                        int bri_num =Analyze_Coordinates();
                        int num = Analyze_Music_Coordinates();
					
                        //播放或者暂停
                        if(num == 1)
                        {							
                            usleep(50000);							
                            Music_Play(music_head);
                        }
                        //上一首
                        else if(num == 2 && mplayer == 1)
                        {
                            usleep(50000);
                            Music_prev(music_head);
                        }
                        //下一首
                        else if(num == 3 && mplayer == 1)
                        {
                            usleep(50000);
                            Music_Next(music_head);
                        }
                        //后退
                        else if(num == 4 && mplayer == 1)
                        {							
                            usleep(50000);
                            Music_Seek(-5);
                        }
                        //快进
                        else if(num == 5 && mplayer == 1)
                        {
                            usleep(50000);							
                            Music_Seek(5);
                        }
                        //上一页
                        else if(num == 6 && mplayer == 1)
                        {
                            usleep(50000);
                            Prev_List(&musiclist , music_head);
                        }
                        //下一页
                        else if(num == 7 && mplayer == 1)
                        {
                            usleep(50000);
                            Next_List(&musiclist , music_head);
                        }
                        //倍速
                        else if(num == 8 && mplayer == 1)
                        {
                            usleep(50000);
                            Music_Speed(music_head);
                        }
                        //静音
                        else if(num == 9 && mplayer == 1)
                        {
                            Mute_Volume();
                        }
                        //增加亮度
                        else if(bri_num == 0 && x_s>0 && x_e<80 && y_s>80 && y_e<420)
                        {
                            printf("上划\n");
                            VBright(5);
                        }
                        //减少亮度
                        else if(bri_num == 1 && x_s>0 && x_e<80 && y_s>80 && y_e<420)
                        {
                            printf("下划\n");
                            VBright(-5);
                        }
                        //寻找mv
                        else if(x_s>590 && x_e<650 && y_s>420 && y_e<480)
                        {
                            printf("正在寻找mv\n");
                            Mv_Play(music_head , video_head);
                        }
                        else if(x_s > 0 && x_s < 80 && y_s > 0 && y_e < 80)
                        {
                            //用户点击了返回
                            printf("点击了返回\n");
                            music_init = 1;
                            //退出
                            ExitTid();
                            break;
                        }

                    }
                }
                //返回
                else if(x_s > 0 && x_s < 80 && y_s > 0 && y_e < 80)
                {
                    //用户点击了返回
                    printf("返回主界面成功\n");
                    break;
                }
            }
        }
        else{
            break;
        }
    }

    //显示主界面
    ShowJpg(map, "pic/UI/exit.jpg" , 0 , 0 , 1 , 1 );
    //退出
    void ExitLcd();
    //销毁链表
    List_Destory(video_head);
    List_Destory(music_head);
    return 0;
}